package com.zhangyongyi.carhome.fragment.findcar.episode;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.adapter.findcar.episode.EpisodeAdapter;
import com.zhangyongyi.carhome.db.DaoHelper;
import com.zhangyongyi.carhome.entity.episode.EpisodeEntity;
import com.zhangyongyi.carhome.entity.greendao.EpisodeList;
import com.zhangyongyi.carhome.tools.DividerItemDecoration;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by dllo on 16/1/21.
 * 段子
 */
public class EpisodeFragment extends Fragment {
    private RecyclerView recyclerView;
    private EpisodeAdapter adapter;
    private RequestQueue queue;
    private DaoHelper helper;




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_episode, null);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.rv_episode);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(gridLayoutManager);

        adapter = new EpisodeAdapter();
        //分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setAdapter(adapter);
        initData();
        helper = DaoHelper.getInstance();
    }

    private void initData() {
        queue = Volley.newRequestQueue(getActivity());
        String url = "http://120.55.151.67/weibofun/weibo_list.php?apiver=10701&category=weibo_jokes&page=0&page_size=30&max_timestamp=-1&latest_viewed_ts=-1&platform=iphone&appver=1.9&buildver=1.9.4&udid=0FD678F7-07AA-4EBF-BB39-EB79EAB40753&sysver=7.0.4";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                EpisodeEntity ee = gson.fromJson(response, EpisodeEntity.class);
                if (ee != null) {
                    List<EpisodeList> episodeLists = ee.getItems();
                    if (episodeLists != null && episodeLists.size() > 0) {
                        adapter.addData(episodeLists);
                        helper.addEpisodeList(episodeLists);
                    }

                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                adapter.addData(helper.seacherEpisodeList());
            }
        }) {


            /**防止乱码 **/
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }

        };
        queue.add(request); // 将请求加入到请求队列中
    }




}
